package com.example.car


import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import androidx.activity.viewModels


import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.car.adapter.ShengAdapter
import com.example.car.databinding.ActivityCarBinding
import com.example.car.entity.CarAddParam
import com.example.car.vm.CarViewModel
import com.example.common.view.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

data class CarplateEntity(
    val name:String
)
@Route (path = "/car/CarActivity")
@AndroidEntryPoint
class CarActivity : BaseMvvmActivity<ActivityCarBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_car
    val shengAdapter=ShengAdapter()
    var index:Int=0
    private val carViewModel:CarViewModel by viewModels()
    override fun initView() {
        super.initView()
        val editText: Array<EditText> = arrayOf(mDataBinding.et1,mDataBinding.et2,mDataBinding.et3,mDataBinding.et4,mDataBinding.et5,mDataBinding.et6,mDataBinding.et7,mDataBinding.et8)
        //车牌
        var carList= arrayOf("鄂","京","津","冀","晋","蒙","辽","吉","黑","沪","苏","浙","皖","闽","赣","鲁","豫","鄂","湘","粤","桂","琼","渝","川","贵","云","藏","陕","甘","青","宁","新"," "," ","X")
        var carplateEntity=ArrayList<CarplateEntity>()
        for (i in carList){
            index=i.length-1
            carplateEntity.add(CarplateEntity(i))
        }
        mDataBinding.rv.adapter=shengAdapter
        mDataBinding.rv.layoutManager=GridLayoutManager(this,7)
        mDataBinding.back.setOnClickListener {
            finish()
        }

        shengAdapter.data.clear()
        shengAdapter.data.addAll(carplateEntity)
        shengAdapter.notifyDataSetChanged()
        mDataBinding.carTime.text= SimpleDateFormat("2026-MM-dd HH:mm:ss").format(System.currentTimeMillis())
        shengAdapter.setOnItemClickListener { adapter, view, position ->
            if (carList[position].equals("X")){
                mDataBinding.et1.text.clear()
                mDataBinding.et2.text.clear()
                mDataBinding.et3.text.clear()
                mDataBinding.et4.text.clear()
                mDataBinding.et5.text.clear()
                mDataBinding.et6.text.clear()
                mDataBinding.et7.text.clear()
                mDataBinding.et8.text.clear()
                mDataBinding.carCode.text=""
            }else{
                editText[0].setText(carList[position])
            }
        }
        for (i in editText.indices){
            editText[i].addTextChangedListener(object :TextWatcher{
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }

                override fun afterTextChanged(s: Editable?) {
                    if (s?.length==1){
                        if (index<editText.size-1){
                            index++
                            editText[index].requestFocus()
                        }
                        if (index==editText.size-1){
                            mDataBinding.carCode.text=mDataBinding.et1.text.toString()+mDataBinding.et2.text.toString()+mDataBinding.et3.text.toString()+mDataBinding.et4.text.toString()+mDataBinding.et5.text.toString()+mDataBinding.et6.text.toString()+mDataBinding.et7.text.toString()+mDataBinding.et8.text.toString()
                        }
                    }else if (s?.length==0){
                        if (index>0){
                            editText[index-1].requestFocus()
                        }
                    }
                }
            })
        }
        mDataBinding.btnAddCar.setOnClickListener {
            if(mDataBinding.carCode.text.isEmpty()){
                ToastUtils.showLong("请输入车牌号")
                return@setOnClickListener
            }else{
                val carAddParam=CarAddParam(
                    id= 0,
                    licensePlate=mDataBinding.carCode.text.toString(),
                    ownerName="玻璃姐",
                    provinceAbbreviation=mDataBinding.et1.text.toString(),
                    userId=1
                )
                carViewModel.getAddCar(carAddParam)
            }
        }
        carViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("添加成功")
            finish()
        }
        carViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }


    }

}